const vm = new Vue({
    el: '#app',
    data: {
       book:[
        {
            id:1,
            name:"《算法导论》",
            date:"2006-9",
            price:85,
            num:1,
        },
        {
            id:2,
            name:"《UNIX编程艺术》",
            date:"2006-2",
            price:59,
            num:1,
        },
        {
            id:3,
            name:"《编程珠玑》",
            date:"2008-10",
            price:39,
            num:1,
        },
        {
            id:4,
            name:"《代码大全》",
            date:"2006-3",
            price:128,
            num:1,
        }
       ],
       isShow:true,
       isShow2:false
    },
    computed: {
        sumPrice:function(){
            var sum=0;
            for(var i=0;i<this.book.length;i++){
                sum+=this.book[i].price*this.book[i].num;
            }
            return sum;
        }
    },
    methods: {
        reduce(index){
            console.log("减少");
            if( this.book[index].num>=1){
                this.book[index].num--;
            }
        },
        add(index){
            console.log("增加");
            this.book[index].num++;
        },
        remove(index){
            console.log("移除"+index);
            this.book.splice(index,1);
            if(this.book.length==0){
                // 当数组为空设置表格不再显示，并设置提示显示
                this.isShow=false;
                this.isShow2=true;
            }
        },
    }
})